﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Ajax.Common
{
    /// <summary>
    /// 服务器控件处理类
    /// </summary>
    public class ServerControl
    {
        /// <summary>
        /// 取得服务器控件生成的HTML代码
        /// </summary>
        /// <param name="ctl"></param>
        /// <returns></returns>
        public static string RenderControlAsString(Control ctl)
        {
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                ctl.RenderControl(writer);
                return sw.ToString();
            }
        }

        #region 绑定对象数据源

        /// <summary>
        /// 绑定对象数据源
        /// </summary>
        /// <param name="list">DropDownList 、ComboBox 等等继承 ListControl 的数据控件。</param>
        /// <param name="dataSource">数据源。</param>
        /// <param name="textfield">文本。</param>
        /// <param name="valuefield">值。</param>
        /// <param name="firstListItem">第一项是否是空字符串。true是，false否。</param>
        public static void BindDropDownList(ListControl list, object dataSource, string textfield, string valuefield, ListItem firstListItem)
        {
            list.DataSource = dataSource;
            list.DataTextField = textfield;
            list.DataValueField = valuefield;
            list.DataBind();

            if (firstListItem != null)
            {
                list.Items.Insert(0, firstListItem);
            }

        }
        #endregion

        #region 绑定树形控件
        public static void BindTreeView(TreeView TreeView1, DataTable dt, string text, string value, string PID, string RootValue)
        {
            BindTreeView(TreeView1, dt, text, value, PID, RootValue, string.Empty);
        }
        public static void BindTreeView(TreeView TreeView1, DataTable dt, string text, string value, string PID, string RootValue, string Url)
        {
            foreach (DataRow dr in dt.Select(string.Format("{0}='{1}'", PID, RootValue)))
            {
                TreeNode Node = new TreeNode(dr[text].ToString(), dr[value].ToString());
                if (Url.Length > 0)
                {
                    Node.NavigateUrl = dr[Url].ToString();
                }
                TreeView1.Nodes.Add(Node);

                addNode(TreeView1, dt, text, value, PID, dr[value].ToString(), Url, Node);
            }
        }

        private static void addNode(TreeView TreeView1, DataTable dt, string text, string value, string PID, string PValue, string url, TreeNode tn)
        {
            foreach (DataRow dr in dt.Select(string.Format("{0}='{1}'", PID, PValue)))
            {
                TreeNode Node = new TreeNode(dr[text].ToString(), dr[value].ToString());
                if (url.Length > 0)
                {
                    Node.NavigateUrl = dr[url].ToString();
                }
                tn.ChildNodes.Add(Node);

                addNode(TreeView1, dt, text, value, PID, dr[value].ToString(), url, Node);
            }
        }
        #endregion
    }
}